package webshop.beans.admin;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import database.entity.ItemGroup;
import database.entity.ShopImage;
import database.entity.ShopItem;
import database.entitymanager.ItemGroupManager;
import database.entitymanager.ShopImageManager;
import database.entitymanager.ShopItemManager;

/**
 * 
 * @author Stefan Martinov <stefan.martinov@gmail.com> Bean za admin forme ,
 *         koristi se kao request scope bean , gde se na formi stavlja kao
 *         keep-alive
 * 
 *         Sadrzi u sebi i UploadBean koji ima za ulogu da se brine o
 *         uploadovanim file-ovima koji se kasnije u transakciji namesti da
 *         persistuje
 * 
 */
public class ShopItemsBean extends ShopItem {

	private static final long serialVersionUID = 1L;
	private int scrollerPage = 1;
	private ShopItem selected = null;
	private boolean viewMode = true;
	private boolean editMode = false;
	private boolean addMode = false;
	private List<SelectItem> groupSelection;
	private List<String> selectedGroups;
	private List<ShopItem> items;
	private int maxDisplayCount;
	private static final int MAX_DISPLAY_COUNT = 7;
	private FUBean upload;

	public List<SelectItem> getGroupSelection() {
		return groupSelection;
	}

	public void setGroupSelection(List<SelectItem> groupSelection) {
		this.groupSelection = groupSelection;
	}

	public void setScrollerPage(int scrollerPage) {
		// Popunjavanje elemenata koji ce se vracati+
		items = new ArrayList<ShopItem>();
		for(int i=0;i<ShopItemManager.getShopItemCount();i++)
			items.add(null);
		
		int index=(scrollerPage-1)*MAX_DISPLAY_COUNT;
		for(ShopItem si:ShopItemManager.findShopItemEntities(MAX_DISPLAY_COUNT, (scrollerPage - 1) * MAX_DISPLAY_COUNT)){
			items.set(index,si);
			index++;
		}
	
	
		this.scrollerPage = scrollerPage;
	}

	public int getScrollerPage() {
		return scrollerPage;
	}

	public void setSelected(ShopItem selected) {
		this.selected = selected;
		upload.setShopItem(selected);
	}

	public ShopItem getSelected() {
		return selected;
	}

	public List<ShopItem> getItems() {
		return items;
	}

	public void setItems(List<ShopItem> items) {
		this.items = items;
	}

	@PostConstruct
	public void init() {
		maxDisplayCount = MAX_DISPLAY_COUNT;
		items = new ArrayList<ShopItem>();
		for(int i=0;i<ShopItemManager.getShopItemCount();i++)
			items.add(null);
		
		int index=(scrollerPage-1)*MAX_DISPLAY_COUNT;
		for(ShopItem si:ShopItemManager.findShopItemEntities(MAX_DISPLAY_COUNT, (scrollerPage - 1) * MAX_DISPLAY_COUNT)){
			items.set(index,si);
			index++;
		}
		
		groupSelection = new ArrayList<SelectItem>();
		selectedGroups = new ArrayList<String>();
		upload = new FUBean();
		for (ItemGroup ig : ItemGroupManager.findItemGroupEntities()) {
			groupSelection.add(new SelectItem(ig.getItemgroupid(), ig.getItemgroupname()));
		}
	}

	public void setSelectedGroups(List<String> selectedGroups) {
		this.selectedGroups = selectedGroups;
		List<ItemGroup> newItemGroup = new ArrayList<ItemGroup>();
		for (String id : selectedGroups) {
			newItemGroup.add(ItemGroupManager.findItemGroup(Integer.parseInt(id)));
		}
		selected.setItemGroupList(newItemGroup);

	}

	public List<String> getSelectedGroups() {
		selectedGroups.clear();
		// Podesen EAGER
		if (selected.getItemid() != null) {
			for (ItemGroup ig : selected.getItemGroupList()) {
				selectedGroups.add(String.valueOf(ig.getItemgroupid()));
			}
		}
		return selectedGroups;
	}

	public void setViewMode(boolean viewMode) {
		this.viewMode = viewMode;
	}

	public boolean isViewMode() {
		return viewMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setAddMode(boolean addMode) {
		this.addMode = addMode;
	}

	public boolean isAddMode() {
		return addMode;
	}

	// ACtions
	public void editMode(ActionEvent event) {
		setViewMode(false);
		setEditMode(true);
		setAddMode(false);
	}

	public void addMode(ActionEvent event) {
		selected = new ShopItem();
		upload.setShopItem(selected);
		setViewMode(false);
		setEditMode(true);
		setAddMode(true);
	}

	public void viewMode(ActionEvent event) {
		setViewMode(true);
		setEditMode(false);
		setAddMode(false);
	}

	/**
	 * Sled dogadjaja
	 * 
	 * @param event
	 */
	public void save(ActionEvent event) {
		EntityManager em = ShopItemManager.getEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();

			if (addMode) {
				ShopItemManager.create(selected);
				items.add(selected);
			}

			List<ShopImage> sil = new ArrayList<ShopImage>();
			for (ShopImage si : upload.getFiles()) {
				si.setShopItem(selected);
				if (si.getShopimageid() == null)
					ShopImageManager.create(si);
				sil.add(si);
			}
			selected.setShopImageList(sil);

			if (editMode) {
				ShopItemManager.edit(selected);
			}

			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		}

		viewMode(event);
	}

	public void setMaxDisplayCount(int maxDisplayCount) {
		this.maxDisplayCount = maxDisplayCount;
	}

	public int getMaxDisplayCount() {
		return maxDisplayCount;
	}

	public void setUpload(FUBean upload) {
		this.upload = upload;
	}

	public FUBean getUpload() {
		return upload;
	}

}
